﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Water.Architecture2.IDAL;
using System.ComponentModel;
using System.Reflection;
using System.Drawing.Design;
using Water.Resources.Globalize;
using System.Web.UI.WebControls;
using System.Web;

namespace Water.Web.Controls
{
    [ToolboxData("<{0}:ObjectHierarchicalDataSource runat=server></{0}:ObjectHierarchicalDataSource>")]
    [PersistChildren(false), ParseChildren(true), ControlBuilder(typeof(DataSourceControlBuilder))]
    public class ObjectHierarchicalDataSource : HierarchicalDataSourceControl
    {
        #region private field

        private object typeInstance;
        private MethodInfo method;

        private string dataIDField;
        private string dataParentIDField;
        private string isLeafField;
        private string typeName;
        private Type type;
        private string rootParentID;

        ParameterCollection selectParameters;
        ParameterCollection rootParameters;

        #endregion


        #region IHierarchicalDataSource 成员


        #endregion
        [Browsable(true), Category("Data"), GlobalizeDescription("TypeName")]
        public string TypeName
        {
            get 
            { 
                if(string.IsNullOrEmpty(this.typeName))
                    throw new HttpException(string.Format(ExceptionResource.ExceptionPropertyNotFound, this.ID, "TypeName"));
                return typeName; 
            }
            set { typeName = value; }
        }

        [Browsable(true), Category("Data"), GlobalizeDescription("DataIDField")]
        public string DataIDField
        {
            get 
            {
                if (string.IsNullOrEmpty(this.dataIDField))
                    throw new HttpException(string.Format(ExceptionResource.ExceptionPropertyNotFound, this.ID, "DataIDField"));
                return dataIDField; 
            }
            set { dataIDField = value; }
        }

        [Browsable(true), Category("Data"), GlobalizeDescription("DataParentIDField")]
        public string DataParentIDField
        {
            get 
            {
                if (string.IsNullOrEmpty(this.dataParentIDField))
                    throw new HttpException(string.Format(ExceptionResource.ExceptionPropertyNotFound, this.ID, "DataParentIDField"));
                return dataParentIDField; 
            }
            set { dataParentIDField = value; }
        }

        [Browsable(true), Category("Data"), GlobalizeDescription("IsLeafField"), DefaultValue("0")]
        public string IsLeafField
        {
            get { return isLeafField; }
            set { isLeafField = value; }
        }

        [Browsable(true), Category("Data"), GlobalizeDescription("RootParentID"), DefaultValue("0"), Bindable(true)]
        public string RootParentID
        {
            get 
            {
                if (string.IsNullOrEmpty(this.rootParentID) && this.rootParameters==null)
                    throw new HttpException(string.Format(ExceptionResource.ExceptionRootParentAndRootParametersNotFound, this.ID, "RootParameters", "RootParentID"));
                return rootParentID; 
            }
            set { rootParentID = value; }
        }

        //ensure varible be initialized
        private void EnsureInit()
        {
            if (!string.IsNullOrEmpty(this.TypeName) && !DesignMode)
            {
                if (null == type)
                    type = System.Web.Compilation.BuildManager.GetType(this.TypeName, true, true);
                if (null == typeInstance)
                    typeInstance = Activator.CreateInstance(type);
                if (null == method)
                    method = type.GetMethod("FindToDataSet", new Type[] { typeof(WhereFields) });
            }
        }


        protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath)
        {
            EnsureInit();
            ObjectHierarchicalDataSourceView _ohdsv = new ObjectHierarchicalDataSourceView(this.DataIDField, this.DataParentIDField,this.IsLeafField, viewPath, this.RootParentID, typeInstance, method, this.Context, this, this.SelectParameters,this.RootParameters);
            return _ohdsv;
        }

        [Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, System.Design", typeof(UITypeEditor)), DefaultValue((string)null), MergableProperty(false), PersistenceMode(PersistenceMode.InnerProperty), GlobalizeCategory("Data"), GlobalizeDescription("SelectParameters")]
        public ParameterCollection SelectParameters
        {
            get { return selectParameters; }
            set { selectParameters = value; }
        }

        [Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, System.Design", typeof(UITypeEditor)), DefaultValue((string)null), MergableProperty(false), PersistenceMode(PersistenceMode.InnerProperty), GlobalizeCategory("Data"), GlobalizeDescription("RootParameters")]
        public ParameterCollection RootParameters
        {
            get 
            {
                if (string.IsNullOrEmpty(this.rootParentID) && this.rootParameters == null)
                    throw new HttpException(string.Format(ExceptionResource.ExceptionRootParentAndRootParametersNotFound, this.ID, "RootParameters", "RootParentID"));
                return rootParameters;
            }
            set { rootParameters = value; }
        }
  

    }
}
